/*
 * @Author: HaiF
 * @Date: 2021-01-01 20:26:50
 * @LastEditTime: 2021-04-21 00:33:37
 * @LastEditors: Please set LastEditors
 * @Description:
 * @FilePath: \adminPC\src\router\asyncRouter.js
 * @可以输入预定的版权声明、个性签名、空行等
 */
/* eslint-disable */
import Layout from '@/layout'
/*
const asyncRouter = [
  {
    path: '/tenant',
    component: Layout,
    redirect: 'tenant',
    meta: { title: '租户管理', icon: 'user', code: 'SYSTEM_TENANT' },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/tenant/index'),
        meta: { title: '租户', icon: 'user', code: 'SYSTEM_TENANT_INDEX' }
      }
    ]
  },
  {
    path: '/datapower',
    component: Layout,
    redirect: 'datapower',
    meta: { title: '数据权限', icon: 'user', code: 'SYSTEM_DATAPOWER' },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/dataPower/index'),
        meta: { title: '数据权限', icon: 'user', code: 'SYSTEM_DATAPOWER_INDEX' }
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    redirect: 'setting',
    meta: { title: '系统管理', icon: 'setting', code: 'SYSTEM' },
    children: [
      {
        path: 'client',
        name: 'client',
        component: () => import('@/views/client/index'),
        meta: { title: '客户端管理', icon: 'user', code: 'SYSTEM_CLIENT' }
      },
      {
        path: 'org',
        name: 'org',
        component: () => import('@/views/org/index'),
        meta: { title: '部门管理', icon: 'user', code: 'SYSTEM_ORG' }
      },
      {
        path: 'menu',
        name: 'menu',
        component: () => import('@/views/menus/index'),
        meta: { title: '菜单列表', icon: 'user', code: 'SYSTEM_FUNCTION' }
      },
      {
        path: 'file',
        name: 'file',
        component: () => import('@/views/menus/index'),
        meta: { title: '文件管理', icon: 'user', code: 'SYSTEM_FILE' }
      },
      {
        path: 'user',
        name: 'user',
        component: () => import('@/views/user/people'),
        meta: { title: '用户管理', icon: 'user', code: 'SYSTEM_USER' }
      },
      {
        path: 'role',
        name: 'role',
        component: () => import('@/views/role/index'),
        meta: { title: '角色管理', icon: 'user', code: 'SYSTEM_ROLE' }
      },
      {
        path: 'param',
        name: 'param',
        component: () => import('@/views/params/index'),
        meta: { title: '参数管理', icon: 'user', code: 'SYSTEM_PARAMS' }
      },
      {
        path: 'city',
        name: 'city',
        component: () => import('@/views/adminArea/index'),
        meta: { title: '行政区管理', icon: 'user', code: 'SYSTEM_CITY' }
      },
      {
        path: 'dict',
        name: 'dict',
        component: () => import('@/views/dictionaries/dicIndex'),
        meta: { title: '字典管理', icon: 'user', code: 'SYSTEM_DICT' }
      }
    ]
  },

  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: { title: 'External Link', icon: 'link' }
      }
    ]
  },

  { path: '*', redirect: '/404', hidden: true }
]
 */
const asyncRouterMap = [
  {
    title: '租户管理',
    code: ['SYSTEM_TENANT'],
    component: () => import('@/views/tenant/index')
  },
  {
    title: '数据权限',
    code: ['SYSTEM_DATAPOWER_INDEX', 'SYSTEM_DATASCOPE'],
    component: () => import('@/views/dataPower/index')
  },
  {
    code: ['SYSTEM_CLIENT'],
    title: '客户端管理',
    component: () => import('@/views/client/index')
  },
  {
    code: ['SYSTEM_ORG'],
    title: '部门管理',
    component: () => import('@/views/org/index')
  },
  {
    code: ['SYSTEM_FUNCTION'],
    title: '菜单列表',
    component: () => import('@/views/menus/index')
  },
  {
    title: '用户管理',
    code: ['SYSTEM_USER'],
    component: () => import('@/views/user/people')
  },
  {
    title: '角色管理',
    code: ['SYSTEM_ROLE'],
    component: () => import('@/views/role/index')
  },
  {
    title: '参数管理',
    code: ['SYSTEM_PARAMS'],
    component: () => import('@/views/params/index')
  },
  {
    title: '行政区管理',
    code: ['SYSTEM_CITY'],
    component: () => import('@/views/adminArea/index')
  },
  {
    title: '字典管理',
    code: ['SYSTEM_DICT'],
    component: () => import('@/views/dictionaries/dicIndex')
  },
  // {
  //   path: 'external-link',
  //   component: Layout,
  //   $_join: true,
  //   children: [

  //     {
  //       path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
  //       meta: { title: 'External Link', icon: 'link' }
  //     }
  //   ]
  // },

  { path: '*', redirect: '/404', hidden: true, $_join: true }
]

function filterAsyncRoutes(router, child) {
  const routes = asyncRouterMap
  const res = []
  router.forEach((menu, k) => {
    const { functionName, icon, children = [], code, url, id } = menu

    const menuItem = routes.find(i => i.code && i.code.includes(code)) || /^http/.test(url)
    if (!children.length && !menuItem) return
    const path =/*  menuItem || */ code.toString().replace(/,/g, '')
    let item = {
      path: `${url}`,
      name: url,
      component: menuItem && menuItem.component || Layout,
      meta: { title: functionName, icon: icon || 'user', code, id }
    }
    // 判断是否是 链接
    if(/^http/.test(url)) {
      item = {
        path: `${url}`,
        meta: { title: functionName, icon: icon || 'user', code, id }
      }
    }

    if (children.length) {
      const childrens = filterAsyncRoutes(children, true)
      item.children = childrens
    }
    if(!child&&!children.length){
      item = {
        path: `${path}`,
        name: `${path}`,
        component: Layout,
        meta: { title: functionName, icon: icon || 'user', code, id },
        children : filterAsyncRoutes([menu], true)
      }
      // item.component =  Layout
      // item.path = `/${path}`,
      // item.name = `${path}`,
      // item.children = filterAsyncRoutes([menu], true)
    }
    res.push(item)
  })
  return res
}

export default function(serveMenus = []) {
  const routers = filterAsyncRoutes(serveMenus) || []
  const localRoute = asyncRouterMap
  localRoute.forEach(i => {
    const { $_join } = i
    if ($_join) {
      routers.push(i)
    }
  })
  return routers
}

export function routerMapToWeb() {
  const resole = []
  const routerMap = asyncRouterMap
  routerMap.forEach((i, k) => {
    const { title, code: codeList = [], component, $_join } = i
    const code = codeList.toString().replace(/,/g, '')
    const item = {
      path: `/${code}`,
      name: code,
      component: Layout,
      meta: { title, icon: 'user' },
      children: [{
        path: code,
        name: code,
        component,
        meta: { title, icon: 'user' }
      }]
    }
    if (!$_join) {
      resole.push(item)
    }
  })
  return resole
}
